import LoadManager from "../../manager/LoadManager";
import CallModel from "../../model/CallModel";
import SceneManager from "../../manager/SceneManager";
import HeroData from "../../data/HeroData";
import MenaryModel from "../../model/MenaryModel";

const {ccclass, property} = cc._decorator;

@ccclass
export default class CallSuccess extends cc.Component {
    @property(cc.Node)
    avatar: cc.Node = null;
    @property(cc.Node)
    CallMenu: cc.Node = null;
    @property(cc.Node)
    HeroMessage: cc.Node = null;
    @property(cc.Node)
    heroHead:cc.Node = null;
    @property(cc.Node)
    heroQuality:cc.Node = null;
    @property(cc.Label)
    tips:cc.Label = null;



    @property(cc.Label)
    qulityType:cc.Label = null;
    @property(cc.Label)
    attack:cc.Label= null;
    @property(cc.Label)
    defend:cc.Label = null;
    @property(cc.Label)
    hp:cc.Label = null;
    @property(cc.Label)
    mp:cc.Label = null;
    @property(cc.Label)
    level:cc.Label = null;
    @property(cc.Label)
    nextQulityType:cc.Label = null;
    @property(cc.Label)
    nextAttack:cc.Label= null;
    @property(cc.Label)
    nextDefend:cc.Label = null;
    @property(cc.Label)
    nextHp:cc.Label = null;
    @property(cc.Label)
    nextMp:cc.Label = null;
    @property(cc.Label)
    nextLevel:cc.Label = null;
    @property(cc.Label)
    txt_name:cc.Label = null;


    private static _instance:CallSuccess;
    public initPerson(){
        let callHeroObj:HeroData = CallModel.instance.callHeroObj;
        this.avatar.scaleX = 0.5;
        this.avatar.scaleY = 0.5;
        this.avatar.x = 290
        this.avatar.y = 796
        this.avatar.resumeAllActions();
        this.txt_name.string = callHeroObj.name
        this.qulityType.string = callHeroObj.qualityString
        this.attack.string = String(callHeroObj.cur_atk);
        this.defend.string = String(callHeroObj.cur_def);
        this.hp.string = String(callHeroObj.cur_hp);
        this.mp.string = String(callHeroObj.cur_mp);
        this.level.string = String("Lv"+callHeroObj.lv);

        this.nextQulityType.string = callHeroObj.nextQualityHeroData.qualityString
        this.nextAttack.string = String(callHeroObj.nextQualityHeroData.cur_atk);
        this.nextDefend.string = String(callHeroObj.nextQualityHeroData.cur_def);
        this.nextHp.string = String(callHeroObj.nextQualityHeroData.cur_hp);
        this.nextMp.string = String(callHeroObj.nextQualityHeroData.cur_mp);
        this.nextLevel.string = String("Lv"+callHeroObj.nextQualityHeroData.lv);
        let id:string = callHeroObj.avatarId;
        let len:number = 4-id.length;
        for (let index = 0; index < len; index++) {
            id = "0"+id;  
        }
        this.heroQuality.addComponent(cc.Sprite).spriteFrame = LoadManager.instance.getImgResListByUrl("image/common/item_lv"+callHeroObj.quality+"_bg");
        this.heroHead.addComponent(cc.Sprite).spriteFrame = LoadManager.instance.getImgResListByUrl("image/head/r_"+id);
        this.avatar.getComponent(sp.Skeleton).skeletonData = LoadManager.instance.getSkeletonDataByUrl("spine/character/r_"+id);
        this.avatar.getComponent(sp.Skeleton).setAnimation(0,"idle",true); 
        let aciton1:cc.FiniteTimeAction = cc.spawn(cc.scaleTo(1,2,2),cc.moveTo(1,this.avatar.x,512))
        let callFunc:cc.ActionInstant = cc.callFunc(function(){
            this.avatar.getComponent(sp.Skeleton).setAnimation(0,"celebrate",true);
        },this)
        let action:cc.ActionInterval = cc.sequence(aciton1,callFunc)
        this.avatar.runAction(action)
        
        
    }
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.node.on(cc.Node.EventType.TOUCH_END,function(event){
            this.HeroMessage.active = true;
            this.node.on(cc.Node.EventType.TOUCH_END,function(event){
                MenaryModel.instance.onEnterMenary(function() {
                    SceneManager.instance.goto('MenaryScene');
                })
            },this);
        },this);
    }
    start(){
        this.initPerson();
        this.tips.schedule(function() {
            // 这里的 this 指向 component
            this.active = false
        }, 0.2);
        this.tips.schedule(function() {
            // 这里的 this 指向 component
            this.active = true
        }, 0.4);
        this.HeroMessage.active = false;
    }
    update (dt){
        
    }
}
